package com.songtech.config;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;


@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig  implements EnvironmentAware {

	private RelaxedPropertyResolver propertyResolver;
	
	
	/**
	 * 配置mybatis扫描路径
	 * @return
	 */
	@Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        //对应<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        //对应<property name="basePackage" value="xxx.xxx.dao" />
        mapperScannerConfigurer.setBasePackage(propertyResolver.getProperty("basePackge"));
        return mapperScannerConfigurer;
    }

	@Override
	public void setEnvironment(Environment environment) {
		 this.propertyResolver = new RelaxedPropertyResolver(environment,"mybatis.");
	}
}
